package com.smarthealthring.mm.utils;

import android.util.Log;

import com.smarthealthring.mm.bean.BaseResponse;
import com.smarthealthring.mm.net.ServerException;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by hewei26 on 2017/4/24.
 */

public class RxUtil {

    /**
     * 统一线程处理
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<T, T> scheduleTnansform() {
        return new FlowableTransformer<T, T>() {
            @Override
            public Flowable<T> apply(Flowable<T> flowable) {
                return flowable.subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }

        };
    }

    /**
     * 处理服务器返回的数据，进一步处理错误信息
     *
     * @param <T>
     * @return
     */
    public static <T> FlowableTransformer<BaseResponse<T>, T> handleResult() {
        return new FlowableTransformer<BaseResponse<T>, T>() {
            @Override
            public Flowable<T> apply(@NonNull Flowable<BaseResponse<T>> flowable) {
                return flowable.flatMap(new Function<BaseResponse<T>, Flowable<T>>() {
                    @Override
                    public Flowable<T> apply(@NonNull BaseResponse<T> response) throws Exception {
                        if (response == null) {
                            return Flowable.empty();
                        }
                        if (response.isSuccess()) {
                            Log.e("response_成功===",response.getResultmsg());
                            ToastUtil.shortShow(response.getResultmsg());
                            if (response.getContent() == null) {
                                return Flowable.empty();
                            }
                            return createData(response.getContent());
                        } else {
                            Log.e("response_失败===",response.getResultmsg());
                            return Flowable.error(new ServerException(response.getResultmsg()));
                        }
                    }
                });
            }
        };
    }

    /**
     * 创建成功的数据
     *
     * @param data
     * @param <T>
     * @return
     */
    private static <T> Flowable<T> createData(T data) {
        return Flowable.create(new FlowableOnSubscribe<T>() {
            @Override
            public void subscribe(@NonNull FlowableEmitter<T> flowableEmitter) throws Exception {
                try {
                    flowableEmitter.onNext(data);
                    flowableEmitter.onComplete();
                } catch (Exception e) {
                    flowableEmitter.onError(e);
                }
            }
        }, BackpressureStrategy.BUFFER);
    }
}
